import { Controller, Get, Post, Body, Param, Put, Delete, ParseUUIDPipe, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiParam } from '@nestjs/swagger';
import { Permissions, PermissionsGuard } from '@nbase/nest-mods';
import type { IDict, IPaginationResponse } from '@nbase/shared';
import { DictService } from './dict.service';
import { CreateDictDto } from './dto/create-dict.dto';
import { UpdateDictDto } from './dto/update-dict.dto';
import { QueryDictDto } from './dto/query-dict.dto';

@ApiTags('system/dict')
@ApiBearerAuth('JWT-auth')
@Controller('system/dict')
@UseGuards(PermissionsGuard)
export class DictController {
  constructor(private readonly dictService: DictService) {}

  @ApiOperation({ summary: '获取字典列表' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Permissions('system:dict:query')
  @Get()
  findAll(@Query() query: QueryDictDto): Promise<IPaginationResponse<IDict>> {
    return this.dictService.paginateDicts(query);
  }

  @ApiOperation({ summary: '获取字典详情' })
  @ApiParam({ name: 'id', description: '字典ID', type: String })
  @ApiResponse({ status: 200, description: '获取成功' })
  @ApiResponse({ status: 404, description: '字典不存在' })
  @Permissions('system:dict:query')
  @Get(':id')
  findOne(@Param('id', ParseUUIDPipe) id: string): Promise<IDict> {
    return this.dictService.findById(id);
  }

  @ApiOperation({ summary: '根据编码获取字典' })
  @ApiParam({ name: 'code', description: '字典编码', type: String })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Permissions('system:dict:query')
  @Get('code/:code')
  findByCode(@Param('code') code: string): Promise<IDict | null> {
    return this.dictService.findByCode(code);
  }

  @ApiOperation({ summary: '创建字典' })
  @ApiResponse({ status: 201, description: '创建成功' })
  @ApiResponse({ status: 400, description: '参数错误' })
  @Permissions('system:dict:create')
  @Post()
  create(@Body() createDictDto: CreateDictDto): Promise<IDict> {
    return this.dictService.createDict(createDictDto);
  }

  @ApiOperation({ summary: '更新字典' })
  @ApiParam({ name: 'id', description: '字典ID', type: String })
  @ApiResponse({ status: 200, description: '更新成功' })
  @ApiResponse({ status: 404, description: '字典不存在' })
  @Permissions('system:dict:update')
  @Put(':id')
  update(@Param('id', ParseUUIDPipe) id: string, @Body() updateDictDto: UpdateDictDto): Promise<IDict> {
    return this.dictService.updateDict(id, updateDictDto);
  }

  @ApiOperation({ summary: '删除字典' })
  @ApiParam({ name: 'id', description: '字典ID', type: String })
  @ApiResponse({ status: 200, description: '删除成功' })
  @ApiResponse({ status: 404, description: '字典不存在' })
  @Permissions('system:dict:delete')
  @Delete(':id')
  async remove(@Param('id', ParseUUIDPipe) id: string): Promise<void> {
    await this.dictService.deleteDict(id);
  }
}

